<?php

namespace app\admin\controller;

use app\admin\exception\DuplicateException;
use think\Db;
use think\Exception;
use think\Request;

class Device extends Backend
{

    protected $model = null;

    protected $beforeActionList = [
        'checkStatus' => ['only' => 'delete'],
    ];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Device();
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        $params = $this->request->param();
        if (!empty($params['device_code'])) {
            $map['device_code'] = ['like', '%' . $this->request->param('device_code') . '%'];
        }
        if ($params['startTime'] && $params['endTime']) {
            $where['create_time'] = ['between', [strtotime($params['startTime']), strtotime($params['endTime'] . ' 23:59:59')]];
        }
        $list = $this->model->where($map)->where($where)->order('id desc')->paginate();
        $this->assign('list', $list);
        $this->assign('params', $params);
        return $this->fetch();
    }

    public function import()
    {
        if ($this->request->ext() != 'json') {
            $this->error("The method just only access 'json' suffix");
        }
        if ($this->request->isPost() && $this->request->ext() == 'json') {
            $params = $this->request->post('data');
            $params = json_decode($params, true);
            $codes = array_map(function ($item) {
                return $item['设备编号'];
            }, $params);

            $result = array_flip($codes);
            if (sizeof($result) != sizeof($codes)) {
                $this->error('设备编号存在重复编号');
            }
            try {
                if (sizeof($params) > 0) {
                    Db::startTrans();
                    $res = $this->model->import($codes);
                    Db::commit();
                    if ($res) {
                        $this->success('导入成功');
                    } else {
                        $this->error('导入失败');
                    }
                } else {
                    $this->error($this->model->getError());
                }
            } catch (DuplicateException $e) {
                Db::rollback();
                $this->result($e->getData(), $e->getCode(), $e->getMessage(), 'json');
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage(), null, $e->getData());
            }
        } else {
            $this->error("The method 'import' is not supported " . $this->request->type());
        }
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 检查设备状态
     * @throws Exception
     */
    protected function checkStatus()
    {
        $sealModel = new \app\admin\model\Seal();
        $seal = $this->model->where('id', $this->request->param('id'))->find();
		//print_r($seal);die;
        if (!$seal) {
            $this->error('设备不存在');
        }
        $size = $sealModel->where("number", $seal->device_code)->count();
        if ($size > 0) {
            $this->error('设备已被使用，无法删除');
        }
    }
}
